//
//  PieView.m
//  xpkc
//
//  Created by TopSage iOS on 14-8-29.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "PieView.h"
//把角度转换为弧度的计算公式
#define PI 3.14159265358979323846
static inline float radians(double degrees) { return degrees * PI / 180; }
@implementation PieView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor=[UIColor clearColor];
    }
    return self;
}
- (void)drawRect:(CGRect)rect
{
    [self paintpie:UIGraphicsGetCurrentContext() start:-90 capacity:self.radios*360 pointx:13 pointy:13 piecolor:GreenColor];
    // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 画圆
    CGContextAddArc(ctx, 13, 13, 10, 0, 2 * M_PI, 0);
        [GreenColor set];
    // 3.渲染 (注意, 画线只能通过空心来画)
    //    CGContextFillPath(ctx);
    CGContextStrokePath(ctx);
}
-(void)paintpie:(CGContextRef)ctx
          start:(double)pieStart
       capacity:(double)pieCapacity
         pointx:(double)x
         pointy:(double)y
       piecolor:(UIColor *)color{
    //起始角度，0-360
    double snapshot_start = pieStart;
    //结束角度
    double snapshot_finish = pieStart+pieCapacity;
    //设置扇形填充色
    CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));
    //设置圆心
    CGContextMoveToPoint(ctx, x, y);
    //以90为半径围绕圆心画指定角度扇形，0表示逆时针
    CGContextAddArc(ctx, x, y, 10,  radians(snapshot_start), radians(snapshot_finish), 0);
    CGContextClosePath(ctx);
    CGContextFillPath(ctx);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
